local s15__hujia = fk.CreateSkill {
  name = "s15__hujia",
}

Fk:loadTranslationTable {
  ["s15__hujia"] = "护驾",
  [":s15__hujia"] = "若你为主帅，当你需要使用或打出【闪】时，你可以令己方前锋选择是否打出一张【闪】（视为由你使用或打出）。",

  ["#s15__hujia-ask"] = "护驾：是否为 %src 打出一张【闪】？",
}

local s15__hujia_spec = {
  anim_type = "defensive",
  priority = 2, -- 设置优先级，确保能正确触发
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(s15__hujia.name) and
          Exppattern:Parse(data.pattern):matchExp("jink") and
          (data.extraData == nil or data.extraData.s15__hujia_ask == nil) and
          (player.seat == 2 or player.seat == 5)) then
      return false
    end

    -- 检查是否有存活的友方前锋
    local room = player.room
    local vanguards = {}
    for _, p in ipairs(room.alive_players) do
      if p:isFriend(player) and p ~= player then
        table.insert(vanguards, p)
      end
    end

    return #vanguards > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local vanguards = {}

    -- 获取所有存活的友方前锋
    for _, p in ipairs(room.alive_players) do
      if p:isFriend(player) and p ~= player then
        table.insert(vanguards, p)
      end
    end

    for _, p in ipairs(vanguards) do
      local params = { ---@type AskToUseCardParams
        skill_name = "jink",
        pattern = "jink",
        prompt = "#s15__hujia-ask:" .. player.id,
        cancelable = true,
        extra_data = { s15__hujia_ask = true }
      }
      local respond = room:askToResponse(p, params)
      if respond then
        respond.skipDrop = true
        room:responseCard(respond)

        local new_card = Fk:cloneCard('jink')
        new_card.skillName = s15__hujia.name
        new_card:addSubcards(room:getSubcardsByRule(respond.card, { Card.Processing }))
        local result = {
          from = player,
          card = new_card,
        }
        if event == fk.AskForCardUse then
          result.tos = {}
        end
        data.result = result
        return true
      end
    end
  end,
}

s15__hujia:addEffect(fk.AskForCardUse, s15__hujia_spec)
s15__hujia:addEffect(fk.AskForCardResponse, s15__hujia_spec)

return s15__hujia
